package Demo;

import java.io.*;
import java.util.Scanner;


public class Test1 {
    public static void main(String[] args) throws IOException {
        Scanner scanener = new Scanner(System.in);
        System.out.println("请输入要复制的原始文件 (完整路径): ");
        String fileName = scanener.nextLine();
        System.out.println("请输入复制到的目标目录 (完整路径): ");
        String newDir = scanener.nextLine();

        File file = new File(fileName);
        File dic = new File(newDir);

        if (!file.isFile()) {
            System.out.println("输入的不是文件!");
            return;
        }
        File patentFile = dic.getParentFile();
        if (!patentFile.isDirectory()) {
            System.out.println("目标文件的父目录不存在");
            return;
        }

        copyFile(file, dic);
    }

    private static void copyFile(File file, File dic) throws IOException {
        try (InputStream is = new FileInputStream(file);
             OutputStream os = new FileOutputStream(dic) ) {
            while (true) {
                byte[] b = new byte[1024];
                int len = is.read(b);
                if (len == -1) {
                    break;
                }
                os.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
